package cn.tedu.csmall.passport.controller;


import cn.tedu.csmall.commons.pojo.vo.PageData;
import cn.tedu.csmall.commons.web.JsonResult;
import cn.tedu.csmall.commons.consts.HttpConsts;
import cn.tedu.csmall.passport.pojo.param.AdminAddNewParam;
import cn.tedu.csmall.passport.pojo.param.AdminLoginInfoParam;
import cn.tedu.csmall.passport.pojo.param.AdminUpdateInfoParam;
import cn.tedu.csmall.passport.pojo.vo.AdminListItemVo;
import cn.tedu.csmall.passport.pojo.vo.AdminStandardVo;

import cn.tedu.csmall.passport.service.AdminService;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

@RestController
@RequestMapping("/admin/")//produces = "application/json; charset=utf-8" 解决返回乱码问题
@Slf4j
@Api(tags = "01.管理员管理模块")
@Validated
public class AdminController {

    @Autowired
    private AdminService adminService;

    // http://localhost:9181/admin/login
    @PostMapping("/login")
    @ApiOperation("管理员登录")
    @ApiOperationSupport(order = 50)
    public JsonResult login(AdminLoginInfoParam adminLoginInfoParam, HttpServletRequest request) {
        log.debug("开始处理【管理员登录】的请求，参数：{}", adminLoginInfoParam);
        String remoteAddr = request.getRemoteAddr();
        String userAgent = request.getHeader(HttpConsts.HEADER_USER_AGENT);
        String jwt = adminService.login(adminLoginInfoParam,remoteAddr,userAgent);
        return JsonResult.ok(jwt);
    }

    // http://localhost:9181/admin/add-new
    @PostMapping("/add-new")
    @ApiOperation("添加管理员")
    @ApiOperationSupport(order = 100)
    public JsonResult addNew(@Valid AdminAddNewParam adminAddNewParam){
        log.debug("开始处理【添加管理员】的请求，参数：{}", adminAddNewParam);
        adminService.addNew(adminAddNewParam);
        return JsonResult.ok();
    }


    // http://localhost:9181/admin/delete
    @PostMapping("/delete")
    @PreAuthorize("hasAuthority('/ams/admin/delete')")
    @ApiOperation("根据ID删除管理员")
    @ApiOperationSupport(order = 200)
    @ApiImplicitParam(name = "id", value = "管理员ID", required = true, dataType = "long")
    public JsonResult delete(@Range(min = 1, message = "根据ID删除管理员失败，请提交合法的ID值！")
                             @RequestParam Long id) {
        log.debug("开始处理【根据ID删除管理员】的请求,参数: {}",id);
        adminService.delete(id);
        return JsonResult.ok();
    }

    // http://localhost:9181/admin/update
    @PostMapping("/update")
    @PreAuthorize("hasAuthority('/ams/admin/update')")
    @ApiOperation("修改管理员详情")
    @ApiOperationSupport(order = 300)
    @ApiImplicitParam(name = "id",value = "管理员id",required = true,dataType = "long")
    public JsonResult update(
            @Range(min = 1,message = "修改的管理员id必须大于0")
            @RequestParam Long id, @Valid AdminUpdateInfoParam adminUpdateInfoParam) {
        log.debug("开始处理【修改管理员详情】的业务，参数：{}", adminUpdateInfoParam);
        adminService.updateInfoById(id,adminUpdateInfoParam);
        return JsonResult.ok();
    }

    // http://localhost:9181/admin/standard
    @GetMapping("/standard")
    @PreAuthorize("hasAuthority('/ams/admin/read')")
    @ApiOperation("根据ID查询管理员详情")
    @ApiOperationSupport(order = 410)
    @ApiImplicitParam(name = "id", value = "管理员ID", required = true, dataType = "long")
    public JsonResult getStandardById(@Range(min = 1, message = "根据ID查询管理员详情失败，请提交合法的ID值！")
                                      @RequestParam Long id) {
        log.debug("开始处理【根据ID查询管理员详情】的请求,参数: {}",id);
        AdminStandardVo result = adminService.getStandardById(id);
        return JsonResult.ok(result);
    }

    // http://localhost:9181/admin/list
    @GetMapping("/list")
    @PreAuthorize("hasAuthority('/ams/admin/read')")
    @ApiOperation("查询管理员列表")
    @ApiOperationSupport(order = 420)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page",value = "页码",paramType = "query")
    })
    public JsonResult list(Integer page){
        log.debug("开始处理【查询管理员列表】的请求，参数:{}",page);
        if (page == null || page < 1){
            page = 1;
        }
        PageData<AdminListItemVo> pageData = adminService.list(page);
        return JsonResult.ok(pageData);
    }
}
